package com.bdqn.controller;

import com.bdqn.pojo.Account;
import com.bdqn.pojo.TransactionRecord;
import com.bdqn.service.AccountService;
import com.bdqn.service.TransactionRecordService;
import com.bdqn.util.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * AccountController
 *
 * @author 武子
 * @date 2024/10/31
 */
@Controller
public class LoginController {

    @Resource
    private AccountService accountService;

    @Resource
    private TransactionRecordService transactionRecordService;

    /**
     * 去登陆页
     *
     * @return
     */
    @GetMapping("/login")
    public String toLogin() {
        return "login";
    }

    /**
     * 登录
     *
     * @param model
     * @param session
     * @param cardno
     * @param password
     * @return
     */
    @PostMapping("/login")
    public String doLogin(Model model, HttpSession session, String cardno, String password) {
        Account login = accountService.login(cardno, password);
        if (login != null) {
            session.setAttribute("login", login);
            return "redirect:/main";
        } else {
            model.addAttribute("msg", "用户名或密码错误，登录失败！");
            return "login";
        }
    }

    /**
     * 去主页
     *
     * @return
     */
    @GetMapping("/main")
    public String main() {
        return "main";
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("login");
        session.invalidate();
        return "login";
    }

    @GetMapping("/toAccounts")
    public String accounts() {
        return "accounts";
    }

    @PostMapping("/doAccounts")
    public String doAccounts(HttpSession session, Model model, HttpServletResponse response, String cardno, Double amount) throws IOException {
        TransactionRecord transactionRecord = new TransactionRecord();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        PrintWriter out = response.getWriter();
        // 获取登录者的信息
        Account login = (Account) session.getAttribute("login");
        // 获取被转账者的信息
        Account income = accountService.income(cardno);
        // 计算消费者的余额
        double con = login.getBalance() - amount;
        // 计算被转账者的余额
        double inc = income.getBalance() + amount;
        // 修改消费者的余额
        int consumption = accountService.consumption(login.getCardno(),con);
        // 修改被转账者的余额
        int deposit = accountService.deposit(income.getCardno(),inc);
        if(consumption == 1 && deposit == 1){
            // 给消费者添加一条交易记录
            transactionRecord.setCardno(login.getCardno());
            transactionRecord.setTransactionDate(String.valueOf(timestamp));
            transactionRecord.setAmount(amount);
            transactionRecord.setBalance(con);
            transactionRecord.setTransactionType("取款");
            int conAdd = transactionRecordService.conAdd(transactionRecord);

            // 给消费者添加一条交易记录
            transactionRecord.setCardno(cardno);
            transactionRecord.setTransactionDate(String.valueOf(timestamp));
            transactionRecord.setAmount(amount);
            transactionRecord.setBalance(inc);
            transactionRecord.setTransactionType("存款");
            int incAdd = transactionRecordService.incAdd(transactionRecord);
            out.print("<script>alert('转账成功');</script>");
        }else {
            out.print("<script>alert('转账失败');</script>");
        }
        return "redirect:/main";
    }

    @RequestMapping ("/transaction")
    public String balance(Model model,String transactionDateA,String transactionDateB,@RequestParam(defaultValue = "1") Integer pageIndex){
        Page<TransactionRecord> page = new Page<>();
        page.setPageSize(2);
        page.setTotalCount(transactionRecordService.count(transactionDateA,transactionDateB));
        page.setPageNo(pageIndex);
        List<TransactionRecord> transactionRecordList = transactionRecordService.listPage(transactionDateA,transactionDateB, page);
        page.setData(transactionRecordList);
        model.addAttribute("page",page);
        model.addAttribute("transactionDateA",transactionDateA);
        model.addAttribute("transactionDateB",transactionDateB);
        return "transaction";
    }

}
